package net.oschina.arvin.admin.web.controller;

import net.oschina.arvin.admin.model.User;
import net.oschina.arvin.admin.service.UserService;
import net.oschina.arvin.admin.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * 用户管理控制类
 *
 * @author Arvin
 * @time 2017/11/21 22:57
 */
@Controller
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/listUI.do")
    public String listUI(ModelMap modelMap) {

        Page<User> page = userService.queryPage(new PageRequest(1, 10));

        modelMap.addAttribute("page", page);
        modelMap.addAttribute("count", page.getTotalElements());
        modelMap.addAttribute("users", page.getContent());
        modelMap.addAttribute("usersJson", JsonUtil.toJson(page.getContent()));

        return "/admin/user/listUI";
    }

    @GetMapping("/detail/{id}.do")
    public String detail(@PathVariable Long id, ModelMap modelMap) {
        User user = userService.findById(id);
        modelMap.addAttribute("user", user);
        return "/admin/user/detailUI";
    }

    @GetMapping("/detailByUname/{uname}.do")
    public String detail(@PathVariable String uname, ModelMap modelMap) {
        User user = userService.findByUname(uname);
        modelMap.addAttribute("user", user);
        return "/admin/user/detailUI";
    }
}
